package com.hutou.chuangke.activity;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.hutou.chuangke.Bean.FansBean;
import com.hutou.chuangke.MainApplication;
import com.hutou.chuangke.R;
import com.hutou.chuangke.net.HttpUtil;
import com.hutou.chuangke.net.RequestThreadPool;
import com.hutou.chuangke.net.http.HttpUrl;
import com.hutou.chuangke.net.http.User;
import com.hutou.chuangke.uitl.CommonUtils;
import com.hutou.chuangke.widget.CircleImageView;
import com.hutou.chuangke.widget.DividerItemDecoration;
import com.hutou.chuangke.widget.SwipeRefreshLayout;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

/**
 * Created by o on 2017/5/26.
 */
public class FansListActivity extends BaseActivity{
    private Context mContext;
    private RecyclerView recyclerviewew;
    private SwipeRefreshLayout swipeRefresh;
    private Toolbar toolbar;
    private boolean ismyself;
    private int cruepage = 1;//页数
    private int pagesize = 10;//页长
    private ArrayList<FansBean> mData = new ArrayList<>();
    private FansAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_fanslist);
        initView();
    }

    private void initView() {
        ismyself = getIntent().getBooleanExtra("ismyself",false);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        recyclerviewew = (RecyclerView) findViewById(R.id.recyclerviewew);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setPadding(0, CommonUtils.getStatusHeight(mContext), 0, 0);
        setSupportActionBar(toolbar);
        final ActionBar ab = getSupportActionBar();
        ab.setHomeAsUpIndicator(R.drawable.actionbar_back);
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setTitle("粉丝");
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        showQuickControl(false);

        recyclerviewew = (RecyclerView) findViewById(R.id.recyclerviewew);
        recyclerviewew.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerviewew.addItemDecoration(new DividerItemDecoration(mContext,DividerItemDecoration.VERTICAL_LIST));
        adapter = new FansAdapter(mContext,mData);
        recyclerviewew.setAdapter(adapter);

        swipeRefresh = (SwipeRefreshLayout) findViewById(R.id.swiperefresh);
        swipeRefresh.setHeaderView(crateHeadview());
        swipeRefresh.setFooterView(crateHeadview());
        swipeRefresh.setOnPullRefreshListener(new SwipeRefreshLayout.OnPullRefreshListener() {
            @Override
            public void onRefresh() {
                swipeRefresh.setRefreshing(true);
                mData.clear();
                loadData();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        swipeRefresh.setRefreshing(false);
                    }
                },2000);
            }

            @Override
            public void onPullDistance(int distance) {}

            @Override
            public void onPullEnable(boolean enable) {}
        });

        swipeRefresh.setOnPushLoadMoreListener(new SwipeRefreshLayout.OnPushLoadMoreListener() {
            @Override
            public void onLoadMore() {
                swipeRefresh.setLoadMore(true);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        swipeRefresh.setLoadMore(false);
                    }
                },2000);
            }

            @Override
            public void onPushDistance(int distance) {

            }

            @Override
            public void onPushEnable(boolean enable) {

            }
        });
        loadData();
    }

    private void loadData(){
        RequestFansList requestFansList = new RequestFansList();
        requestFansList.execute("9");
    }

    /***
     * 获取粉丝列表
     * */
    class RequestFansList extends AsyncTask<String,Void,Boolean> {

        @Override
        protected Boolean doInBackground(String... params) {
            try{
                JsonObject jsonObject = HttpUtil.getResposeJsonObject(
                        User.getUserFansList(params[0], MainApplication.id,MainApplication.sid,cruepage,pagesize));
                JsonArray jsonarray = jsonObject.getAsJsonArray("fansList");
                for(JsonElement element : jsonarray){
                    mData.add(MainApplication.gsonInstance().fromJson(element,FansBean.class));
                }
                String flag = jsonObject.get("flag").getAsString();
                if(flag.equals("1")){
                    return true;
                }
                return false;
            }catch(Exception e){
                e.printStackTrace();
            }
            return false;
        }

        @Override
        protected void onPostExecute(Boolean comlete) {
            if(comlete){
                adapter.setdata(mData);
                adapter.notifyDataSetChanged();
            }else{
                Toast.makeText(mContext,"失败",Toast.LENGTH_SHORT).show();
            }
        }

        public void cancleTask(){
            cancel(true);
            RequestThreadPool.finish();
        }
    }

    private View crateHeadview() {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.loading_circle,null);
        return view;
    }

    Handler handler = new Handler();

    public interface OnItemClickListener{
        void onItemClick(View view,int position);
    }

    class FansAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
        ArrayList<FansBean> mData = new ArrayList<>();
        private Context context;
        public FansAdapter(Context context,ArrayList<FansBean> mData){
            this.mData = mData;
            this.context = context;
        }

        private void setdata(ArrayList<FansBean> mData){
            this.mData = mData;
            notifyDataSetChanged();
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            FansAdapter.ItemHolder myholder = new ItemHolder(LayoutInflater.from(mContext)
                    .inflate(R.layout.item_friend,null));
            return myholder;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            if(holder instanceof ItemHolder){
                if(ismyself) {
                    ((ItemHolder) holder).isfollow_tv.setVisibility(View.GONE);
                }
                ((ItemHolder)holder).name_tv.setText(mData.get(position).getNickname());
                if(mData.get(position).getSex().equals("m")){
                    ((ItemHolder)holder).sex_iv.setImageDrawable(getResources().getDrawable(R.drawable.me_male));
                }else{
                    ((ItemHolder)holder).sex_iv.setImageDrawable(getResources().getDrawable(R.drawable.me_female));
                }
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
//                        int pos = ((ItemHolder)holder).getLayoutPosition();
//                        mOnItemClickListener.onItemClick(holder.itemView, pos);
                    }
                });
                ImageLoader.getInstance().displayImage(mData.get(position).getUserimgurl(), ((ItemHolder)holder).headiv);
            }

        }

        @Override
        public int getItemCount() {
            return mData.size();
        }

        class ItemHolder extends RecyclerView.ViewHolder{
            TextView name_tv,isfollow_tv;
            ImageView headiv,sex_iv;
            public ItemHolder(View view){
                super(view);
                name_tv = (TextView) view.findViewById(R.id.name_tv);
                headiv = (CircleImageView) view.findViewById(R.id.headiv);
                sex_iv = (ImageView) view.findViewById(R.id.sex_iv);
                isfollow_tv = (TextView) view.findViewById(R.id.isfollow_tv);
            }
        }
    }

}
